package com.bkgy.modularbx.dataCollection.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bkgy.commom.model.QueryDTO;
import com.bkgy.config.BusinessException;
import com.bkgy.modularbx.commonbx.BxCommonService;
import com.bkgy.modularbx.dataCollection.domain.DataCollection;
import com.bkgy.modularbx.dataCollection.dto.SearchCondition;
import com.bkgy.modularbx.dataCollection.mapper.DataCollectionMapper;
import com.bkgy.modularbx.dataCollection.service.DataCollectionService;
import com.bkgy.sys.common.entity.ResultData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.Optional;

/**
 * 数据采集服务实现类
 */
@Service
@Transactional
public class DataCollectionServiceImpl extends ServiceImpl<DataCollectionMapper, DataCollection>
        implements DataCollectionService {

    @Autowired
    private BxCommonService commonService;

    private static final String NAME = "DATA_COLLECTION";

    @Override
    public ResultData<Void> addDataCollection(DataCollection dataCollection) {
        try {
            dataCollection.setToc(new Date());
            dataCollection.setTom(new Date());
            Optional<Integer> maxIndocno = Optional.ofNullable(commonService.getMaxIndocno(NAME));
            dataCollection.setIndocno(maxIndocno.orElse(null));
            save(dataCollection);
            return ResultData.ResultDataSuccessSelf("添加成功", null);
        } catch (Exception e) {
            throw new BusinessException("添加失败");
        }
    }

    @Override
    public ResultData<Page<DataCollection>> pageDataCollection(QueryDTO<SearchCondition> queryDTO) {
        SearchCondition condition = queryDTO.getDto();
        LambdaQueryWrapper<DataCollection> wrapper = new LambdaQueryWrapper<>();
        // 构建查询条件
        if (condition != null) {
            wrapper.like(StringUtils.hasText(condition.getProductionLine()), DataCollection::getProductionLine, condition.getProductionLine());
            wrapper.like(StringUtils.hasText(condition.getL1VariableDescription()), DataCollection::getL1VariableDescription, condition.getL1VariableDescription());
            wrapper.like(StringUtils.hasText(condition.getL1VariableName()), DataCollection::getL1VariableName, condition.getL1VariableName());
            wrapper.like(StringUtils.hasText(condition.getL1VariableAlias()), DataCollection::getL1VariableAlias, condition.getL1VariableAlias());
            wrapper.eq(StringUtils.hasText(condition.getOutDataSource()), DataCollection::getOutDataSource, condition.getOutDataSource());

        }
        Page<DataCollection> page = new Page<>(queryDTO.getPage().getPageIndex(), queryDTO.getPage().getPageSize());
        Page<DataCollection> result = page(page, wrapper);
        return ResultData.ResultDataSuccess(result, (int) result.getTotal());
    }

    @Override
    public ResultData<Void> deleteDataCollection(Integer indocno) {
        DataCollection dataCollection = getById(indocno);
        if (dataCollection == null) {
            throw new BusinessException("要删除的数据字典不存在");
        }
        removeById(indocno);
        return ResultData.ResultDataSuccessSelf("删除成功", null);
    }


    @Override
    public ResultData<Void> updateDataCollection(DataCollection dataCollection) {
        DataCollection existDataCollection = getById(dataCollection.getIndocno());
        if (existDataCollection == null) {
            throw new BusinessException("要修改的数据字典不存在");
        }
        dataCollection.setTom(new Date());
        updateById(dataCollection);
        return ResultData.ResultDataSuccessSelf("修改成功", null);
    }

    @Override
    public ResultData<DataCollection> findById(Integer indocno) {
        DataCollection dataCollection = getById(indocno);
        if (dataCollection == null) {
            throw new BusinessException("数据字典不存在");
        }
        return ResultData.ResultDataSuccess(dataCollection, 1);
    }
}




